package com.neuedu.dao;



import com.neuedu.entity.Emp;
import com.neuedu.entity.Emp;
import com.neuedu.utils.DBUtils;

import java.util.List;
import java.util.Map;

public class EmpDao {

    /**
     * 根据条件查询集合
     * @param where
     * @param params
     * @return
     */
    //where  =      where sss = ?
    public List<Emp> selectList(String where , Object ... params){
        String sql =" select * from emp ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        System.out.println("sql = " + sql);
        List<Emp> emps = DBUtils.selectList(Emp.class, sql, params);

        return emps;
    }

    /**
     * 根据条件查询一个
     * @param where
     * @param params
     * @return
     */
    public Emp selectOne(String where ,Object ... params){
        String sql =" select * from emp ";
        if(where != null && !"".equals(where)){
            sql += where;
        }
        return DBUtils.selectOne(Emp.class, sql, params);
    }


    /**
     * 查询总条数
     * @param where
     * @param params
     * @return
     */
    public long selectCount(String where ,Object ... params){
        String sql =" select * from emp ";
        if(where != null && !"".equals(where)){
            sql += where;
        }

        sql = "select count(1) count from ("+ sql  +") tab   ";
        Map map = DBUtils.selectMap( sql , params);
        long count = (long) map.get("count");
        return count;
    }

    /**
     * 根据数据进行插入
     * @param emp
     * @return
     */
    public int insert(Emp emp){
        String sql = "INSERT INTO `hrbu2021`.`emp` (  `empno`, `ename`,  `job`, `mgr`, `hiredate`,  `sal`, `comm`,  `deptno` )   values(?,?,?,?,?,?,?,?)";
        return DBUtils.update( sql,
                emp.getEmpno(),
                emp.getEname(),
                emp.getJob(),
                emp.getMgr(),
                emp.getHiredate(),
                emp.getSal(),
                emp.getComm(),
                emp.getDeptno()
                );


    }


    /**
     *  批量插入
     * @param empList
     * @return
     */
    public int insertBatch(List<Emp> empList){
        String sql = "INSERT INTO `hrbu2021`.`emp` (  `empno`, `ename`,  `job`, `mgr`, `hiredate`,  `sal`, `comm`,  `deptno` )   values(?,?,?,?,?,?,?,?)";
        int count = 0;
        for (Emp emp : empList) {
            count += DBUtils.update( sql,
                    emp.getEmpno(),
                    emp.getEname(),
                    emp.getJob(),
                    emp.getMgr(),
                    emp.getHiredate(),
                    emp.getSal(),
                    emp.getComm(),
                    emp.getDeptno()
            );

        }
        return count;
    }


    /**
     * 根据主键更新一条
     * @param emp
     * @return
     */
    public int updateById(Emp emp){
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 			");
        sql.append("   `emp`            ");
        sql.append(" SET                ");
        sql.append("   `ename` = ?,     ");
        sql.append("   `job` = ?,       ");
        sql.append("   `mgr` =?,        ");
        sql.append("   `hiredate` = ?,  ");
        sql.append("   `sal` = ?,       ");
        sql.append("   `comm` = ?,      ");
        sql.append("   `deptno` = ?    ");
        sql.append(" WHERE `empno` = ?  ");


        return DBUtils.update( sql.toString(),

                emp.getEname(),
                emp.getJob(),
                emp.getMgr(),
                emp.getHiredate(),
                emp.getSal(),
                emp.getComm(),
                emp.getDeptno(),
                emp.getEmpno());

    }


    /**
     * 根据主键更新一条
     * @param id
     * @return
     */
    public int deleteById(Integer id){
        String sql = " delete from emp  where empno = ?";
        return DBUtils.update( sql, id);

    }


    /**
     * 根据条件删除
     * @param where
     * @param params
     * @return
     */
    public int delete(String where ,Object... params){
        String sql = " delete from emp ";
        if(where == null || "".equals(where)){
            throw new RuntimeException("必须有条件删除");
        }
        sql += where;
        return DBUtils.update( sql, params);

    }

}
